<?php
/**
* Api_Form_Validator_CurrentPassword class for Boomeranfg Referral Network
*
* Api_Form_Validator_CurrentPassword class contains logical part of the forward mail form. It also
* contain server side validation, filtering and security issues which are handled at server side
*
* Custom validator to check if current password submitted by user is correct or not
* 
* @package      Api_Form_Validator
* @copyright    Copyright (c) 2011-2012 Boomerang Referral Network
* @version      $Id: CurrentPassword.php v 1.0.0$
* @since        Class available since Release 1.0.0
* @author       Anindya Roy Chodhury <anindya23@gmail.com>
*/
class Api_Form_Validator_CurrentPassword extends Zend_Validate_Abstract
{
    const NOT_MATCH = 'currentPasswordNotMatch';

    /**
     * Authentication session
     * 
     * @var Zend_Session_Namespace
     */
    private $_userSession;
    
    /**
     * Business Logic Layer of the User Management module
     * 
     * @var Api_Core_Bll_User
     */
    private $_userBll;

    protected $_messageTemplates = array(
        self::NOT_MATCH => 'CurrentPassword is not valid'
    );

    public function __construct()
    {
        $this->_userSession = new Zend_Session_Namespace('identity');
        $this->_userBll = new Api_Core_Bll_User_User();
    }

    /**
     * Custom Validation method of the form that uses 
     * Api_Form_Validator_CurrentPassword validation class
     * 
     * @param string User password
     * @return boolean 
     */
    public function isValid($value)
    {
        $error = false;
        $value = (string) $value;
        $this->_setValue($value);
        if (!$this->_userBll->checkCurrentPassword(
                $this->_userSession->username, $value)) {
            $error = true;
            $this->_error(self::NOT_MATCH);
        }
        return !$error;
    }
}
?>
